﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PageView;
using CommunitySystem.PageView.EmpBasicManager;
using CommunitySystem.Views;
using EHR.Common;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;
using System.IO;

namespace CommunitySystem
{
    public partial class OtherEmpInfo : AsyncCtrl
    {
        public int EmpID;
        public EmpbasicService.Emp_BasicInfoModel empbasic = new EmpbasicService.Emp_BasicInfoModel();
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private EmpbasicService.Emp_BasicInfoModel oldmodel = new EmpbasicService.Emp_BasicInfoModel();
        public OtherEmpInfo(int empID)
        {
            InitializeComponent();
            EmpID = empID;
            orgid.IsEnabled = false;
            ygzt.OnSelectionChanged += new OnSelectionChangedEvent(ygzt_OnSelectionChanged);
            Client.GetDengLuJueSeCompleted += new EventHandler<EmpbasicService.GetDengLuJueSeCompletedEventArgs>(Client_GetDengLuJueSeCompleted);
            Client.GetPersonnelInfoExcelCompleted += new EventHandler<EmpbasicService.GetPersonnelInfoExcelCompletedEventArgs>(Client_GetPersonnelInfoExcelCompleted);
            Client.GetDengLuJueSeAsync(Session.LoginUser.LoginName);
            Client.InsertCheGaiCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_InsertCheGaiCompleted);
            Load();
        }
        void Client_InsertCheGaiCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {

        }
        void ygzt_OnSelectionChanged(object sender, object NewValue, object OldValue)
        {
            if (NewValue.ToString() != "在职")
            {
                biangengriqi.Visibility = System.Windows.Visibility.Visible;
                biangengriqi1.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                biangengriqi.Visibility = System.Windows.Visibility.Collapsed;
                biangengriqi1.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
        public void Load()
        {
            //基本信息
            Client.GetEmp_BasicInfoCompleted += new EventHandler<EmpbasicService.GetEmp_BasicInfoCompletedEventArgs>(Client_GetEmp_BasicInfoCompleted);
            Client.GetEmp_BasicInfoAsync("0", "(" + EmpID + ")","","",0,20,"在职", "");
            LoadDuty();
            LoadResume();
            LoadEducation();
            LoadDegree();
            LoadAssessment();
            LoadReward();
            LoadPunish();
            LoadFamily();
        }
        void Client_GetDengLuJueSeCompleted(object sender, EmpbasicService.GetDengLuJueSeCompletedEventArgs e)
        {
            if (e.Result != "人事干部")
            {
                this.orgid.Visibility = System.Windows.Visibility.Visible;
                tex.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                this.orgid.Visibility = System.Windows.Visibility.Collapsed;
                tex.Visibility = System.Windows.Visibility.Visible;
            }
        }
        //职务
        public void LoadDuty()
        {
            Client.GetEmp_DutysCompleted += new EventHandler<EmpbasicService.GetEmp_DutysCompletedEventArgs>(Client_GetEmp_DutysCompleted);
            Client.GetEmp_DutysAsync("(" + EmpID + ")", 0);
        }
        void Client_GetEmp_DutysCompleted(object sender, EmpbasicService.GetEmp_DutysCompletedEventArgs e)
        {
            if(e.Result != null)
            {
                DGZW.ItemsSource = e.Result;
            }
        }

        //简历
        public void LoadResume()
        {
            Client.GetEmp_ResumeCompleted += new EventHandler<EmpbasicService.GetEmp_ResumeCompletedEventArgs>(Client_GetEmp_ResumeCompleted);
            Client.GetEmp_ResumeAsync("(" + EmpID + ")", 0);
        }
        void Client_GetEmp_ResumeCompleted(object sender, EmpbasicService.GetEmp_ResumeCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                DGJL.ItemsSource = e.Result;
            }
        }
        //学历
        public void LoadEducation()
        {
            Client.GetEmp_EducationModelCompleted += new EventHandler<EmpbasicService.GetEmp_EducationModelCompletedEventArgs>(Client_GetEmp_EducationModelCompleted);
            Client.GetEmp_EducationModelAsync("(" + EmpID + ")", 0);
        }

        void Client_GetEmp_EducationModelCompleted(object sender, EmpbasicService.GetEmp_EducationModelCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                DGXL.ItemsSource = e.Result;
            }
        }
        //学位
        public void LoadDegree()
        {
            Client.GetEmp_DegreeModelCompleted += new EventHandler<EmpbasicService.GetEmp_DegreeModelCompletedEventArgs>(Client_GetEmp_DegreeModelCompleted);
            Client.GetEmp_DegreeModelAsync("(" + EmpID + ")", 0);
        }

        void Client_GetEmp_DegreeModelCompleted(object sender, EmpbasicService.GetEmp_DegreeModelCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                DGXW.ItemsSource = e.Result;
            }
        }

        //考核
        public void LoadAssessment()
        {
            Client.GetEmp_AssessmentModelCompleted += new EventHandler<EmpbasicService.GetEmp_AssessmentModelCompletedEventArgs>(Client_GetEmp_AssessmentModelCompleted);
            Client.GetEmp_AssessmentModelAsync("(" + EmpID + ")", 0);
        }

        void Client_GetEmp_AssessmentModelCompleted(object sender, EmpbasicService.GetEmp_AssessmentModelCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                DGKH.ItemsSource = e.Result;
            }
        }

        //奖惩
        public void LoadReward()
        {
            Client.GetEmp_RewardInfoModelCompleted += new EventHandler<EmpbasicService.GetEmp_RewardInfoModelCompletedEventArgs>(Client_GetEmp_RewardInfoModelCompleted);
            Client.GetEmp_RewardInfoModelAsync("(" + EmpID + ")", 0);
        }

        void Client_GetEmp_RewardInfoModelCompleted(object sender, EmpbasicService.GetEmp_RewardInfoModelCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                DGJC.ItemsSource = e.Result;
            }
        }

        //处分
        public void LoadPunish()
        {
            Client.GetEmp_PunishmentModelCompleted += new EventHandler<EmpbasicService.GetEmp_PunishmentModelCompletedEventArgs>(Client_GetEmp_PunishmentModelCompleted);
            Client.GetEmp_PunishmentModelAsync("(" + EmpID + ")", 0);
        }

        void Client_GetEmp_PunishmentModelCompleted(object sender, EmpbasicService.GetEmp_PunishmentModelCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                DGCF.ItemsSource = e.Result;
            }
        }

        //家庭关系
        public void LoadFamily()
        {
            Client.GetEmp_FamilyRelationModelCompleted += new EventHandler<EmpbasicService.GetEmp_FamilyRelationModelCompletedEventArgs>(Client_GetEmp_FamilyRelationModelCompleted);
            Client.GetEmp_FamilyRelationModelAsync("(" + EmpID + ")", 0);
        }

        void Client_GetEmp_FamilyRelationModelCompleted(object sender, EmpbasicService.GetEmp_FamilyRelationModelCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                DGJT.ItemsSource = e.Result;
            }
        }

        //基本信息
        void Client_GetEmp_BasicInfoCompleted(object sender, EmpbasicService.GetEmp_BasicInfoCompletedEventArgs e)
        {
            if (e.Result != null)
                empbasic = e.Result[0];
            if (empbasic.TuiXiuDateString != null) tuixiu.Text = Convert.ToDateTime(empbasic.TuiXiuDate).ToString("yyyy-MM");
            tuixiu12.Text = empbasic.JuTuiXiuDate.ToString();
            basic.DataContext = empbasic;
            if (empbasic.HasPhoto == true)
            {
                ObservableCollection<byte> context = new ObservableCollection<byte>();
                CommunitySystem.PublicOfficialSelectionService.PublicOfficialSelectionServiceClient client = new PublicOfficialSelectionService.PublicOfficialSelectionServiceClient();
                client.DownLoadFileAsync(new PublicOfficialSelectionService.DownLoadFile() { Content = null, FileName = empbasic.PhotoName, StartPosition = 0, Lenght = 1600, HasFinished = false, PathKey = "photo" });
                client.DownLoadFileCompleted += (sender1, e1) =>
                {
                    CommunitySystem.PublicOfficialSelectionService.DownLoadFile file = e1.Result;
                    if (file == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(empbasic.Name + "的图片错误，请联系管理员!");
                        return;
                    }
                    foreach (byte b in file.Content)
                    {
                        context.Add(b);
                    }
                    if (file.HasFinished == true)
                    {
                        BitmapImage b = new BitmapImage();
                        Stream mm = new MemoryStream(context.ToArray<byte>());
                        //mm.Write(_item.icon, 0, _item.icon.Length);
                        b.SetSource(mm);
                        img.Source = b;
                    }
                    else
                    {
                        file.StartPosition += file.Lenght;
                        client.DownLoadFileAsync(file);
                    }
                };
            }
            oldmodel = new EmpbasicService.Emp_BasicInfoModel
                           {
                               Birthday = empbasic.Birthday,
                               CardNo = empbasic.CardNo,
                               Name = empbasic.Name,
                               Gender = empbasic.Gender,
                               BirthPlace = empbasic.BirthPlace,
                               HouseholdRegister = empbasic.HouseholdRegister,
                               Native = empbasic.Native,
                               Political = empbasic.Political,
                               Health = empbasic.Health,
                               WorkDate = empbasic.WorkDate,
                               RegisterNo = empbasic.RegisterNo,
                               OrganizationType = empbasic.OrganizationType,
                               NowWorkDate = empbasic.NowWorkDate,
                               NativePlace = empbasic.NativePlace,
                               Marriage = empbasic.Marriage,
                               HomeAddress = empbasic.HomeAddress,
                               Phone = empbasic.Phone,
                               EmpIdentity = empbasic.EmpIdentity,
                               PartyTime = empbasic.PartyTime,
                               Special = empbasic.Special,
                               TechnicalPositions = empbasic.TechnicalPositions,
                               PersonnelType = empbasic.PersonnelType,
                               RegisterDate = empbasic.RegisterDate,
                               PositionName = empbasic.PositionName,
                               TuiXiuDate = empbasic.TuiXiuDate,
                               JuTuiXiuDate = empbasic.JuTuiXiuDate,
                               HuoDeFangShi = empbasic.HuoDeFangShi,
                               OrgID = empbasic.OrgID,
                               EmpStatus = empbasic.EmpStatus,
                               StatusDate = empbasic.StatusDate

                           };
        }
        /// <summary>
        /// 保存基本信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(name.Text))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入人员姓名！");
                return;
            }
            if (string.IsNullOrEmpty(cardno.Text))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入人员证件号码！");
                return;
            }
            if (oldmodel.OrgID != empbasic.OrgID && (oldmodel.OrgID != null && oldmodel.OrgID != 0))
            {
                MessageBox.Show("您已修改该人员单位！");
                Client.InsertCheGaiAsync(oldmodel.ID, "更改人员单位", "变动类");
            }
            if (oldmodel.EmpStatus != empbasic.EmpStatus && !string.IsNullOrEmpty(oldmodel.EmpStatus))
            {
                MessageBox.Show("您已修改该人员的状态！");
            }
            if (biangengriqi.Visibility == System.Windows.Visibility.Visible && string.IsNullOrEmpty(empbasic.StatusDate.ToString()))
            {
                MessageBox.Show("请正确填写身份变更时间！");
                return;
            }
            Client.UpdateEmp_BasicInfoCompleted += new EventHandler<EmpbasicService.UpdateEmp_BasicInfoCompletedEventArgs>(Client_UpdateEmp_BasicInfoCompleted);
            Client.UpdateEmp_BasicInfoAsync(empbasic);
        }
        void Client_UpdateEmp_BasicInfoCompleted(object sender, EmpbasicService.UpdateEmp_BasicInfoCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                InsertSystem("编辑");
            }
        }
        private InsertSystemRemark insertsys = new InsertSystemRemark();
        void InsertSystem(string FangShi)
        {
            if (FangShi == "新增")
            {
                insertsys.Insert("人员基本信息", "", "", empbasic.ID, FangShi, "");
            }
            else
            {
                if (oldmodel.Birthday != empbasic.Birthday)
                    insertsys.Insert("人员基本信息", oldmodel.Birthday == null ? null : Convert.ToDateTime(oldmodel.Birthday).ToShortDateString(),
                        empbasic.Birthday == null ? null : Convert.ToDateTime(empbasic.Birthday).ToShortDateString(),
                        empbasic.ID, FangShi, "生日");
                if (oldmodel.BirthPlace != empbasic.BirthPlace)
                    insertsys.Insert("人员基本信息", oldmodel.BirthPlace, empbasic.BirthPlace,
                        empbasic.ID, FangShi, "出生地");
                if (oldmodel.CardNo != empbasic.CardNo)
                    insertsys.Insert("人员基本信息", oldmodel.CardNo, empbasic.CardNo,
                        empbasic.ID, FangShi, "证件号码");
                if (oldmodel.EmpIdentity != empbasic.EmpIdentity)
                    insertsys.Insert("人员基本信息", oldmodel.EmpIdentity, empbasic.EmpIdentity,
                        empbasic.ID, FangShi, "个人身份");
                if (oldmodel.Gender != empbasic.Gender)
                    insertsys.Insert("人员基本信息", oldmodel.Gender, empbasic.Gender,
                        empbasic.ID, FangShi, "性别");
                if (oldmodel.Health != empbasic.Health)
                    insertsys.Insert("人员基本信息", oldmodel.Health, empbasic.Health,
                        empbasic.ID, FangShi, "健康状况");
                if (oldmodel.HomeAddress != empbasic.HomeAddress)
                    insertsys.Insert("人员基本信息", oldmodel.HomeAddress, empbasic.HomeAddress,
                        empbasic.ID, FangShi, "家庭住址");
                if (oldmodel.HouseholdRegister != empbasic.HouseholdRegister)
                    insertsys.Insert("人员基本信息", oldmodel.HouseholdRegister, empbasic.HouseholdRegister,
                        empbasic.ID, FangShi, "户籍所在地");
                if (oldmodel.Marriage != empbasic.Marriage)
                    insertsys.Insert("人员基本信息", oldmodel.Marriage, empbasic.Marriage,
                        empbasic.ID, FangShi, "婚姻状况");
                if (oldmodel.Name != empbasic.Name)
                    insertsys.Insert("人员基本信息", oldmodel.Name, empbasic.Name,
                        empbasic.ID, FangShi, "姓名");
                if (oldmodel.Native != empbasic.Native)
                    insertsys.Insert("人员基本信息", oldmodel.Native, empbasic.Native,
                        empbasic.ID, FangShi, "民族");
                if (oldmodel.NativePlace != empbasic.NativePlace)
                    insertsys.Insert("人员基本信息", oldmodel.NativePlace, empbasic.NativePlace,
                        empbasic.ID, FangShi, "籍贯");
                if (oldmodel.NowWorkDate != empbasic.NowWorkDate)
                    insertsys.Insert("人员基本信息", oldmodel.NowWorkDate == null ? null : Convert.ToDateTime(oldmodel.NowWorkDate).ToShortDateString(),
                        empbasic.NowWorkDate == null ? null : Convert.ToDateTime(empbasic.NowWorkDate).ToShortDateString(),
                        empbasic.ID, FangShi, "进本单位年月");
                if (oldmodel.OrganizationType != empbasic.OrganizationType)
                    insertsys.Insert("人员基本信息", oldmodel.OrganizationType, empbasic.OrganizationType,
                        empbasic.ID, FangShi, "编制类型");
                if (oldmodel.PartyTime != empbasic.PartyTime)
                    insertsys.Insert("人员基本信息", oldmodel.PartyTime == null ? null : Convert.ToDateTime(oldmodel.PartyTime).ToShortDateString(),
                        empbasic.PartyTime == null ? null : Convert.ToDateTime(empbasic.PartyTime).ToShortDateString(),
                        empbasic.ID, FangShi, "进本单位年月");
                if (oldmodel.PersonnelType != empbasic.PersonnelType)
                    insertsys.Insert("人员基本信息", oldmodel.PersonnelType, empbasic.PersonnelType,
                        empbasic.ID, FangShi, "人员类别");
                if (oldmodel.Phone != empbasic.Phone)
                    insertsys.Insert("人员基本信息", oldmodel.Phone, empbasic.Phone,
                        empbasic.ID, FangShi, "住宅电话");
                if (oldmodel.Political != empbasic.Political)
                    insertsys.Insert("人员基本信息", oldmodel.Political, empbasic.Political,
                        empbasic.ID, FangShi, "政治面貌");
                if (oldmodel.PositionName != empbasic.PositionName)
                    insertsys.Insert("人员基本信息", oldmodel.PositionName, empbasic.PositionName,
                        empbasic.ID, FangShi, "现部门名称");
                if (oldmodel.RegisterDate != empbasic.RegisterDate)
                    insertsys.Insert("人员基本信息", oldmodel.RegisterDate == null ? null : Convert.ToDateTime(oldmodel.RegisterDate).ToShortDateString(),
                        empbasic.RegisterDate == null ? null : Convert.ToDateTime(empbasic.RegisterDate).ToShortDateString(),
                        empbasic.ID, FangShi, "公务员登记时间");
                if (oldmodel.RegisterNo != empbasic.RegisterNo)
                    insertsys.Insert("人员基本信息", oldmodel.RegisterNo, empbasic.RegisterNo,
                        empbasic.ID, FangShi, "公务员登记号");
                if (oldmodel.Special != empbasic.Special)
                    insertsys.Insert("人员基本信息", oldmodel.Special, empbasic.Special,
                        empbasic.ID, FangShi, "专长");
                if (oldmodel.WorkDate != empbasic.WorkDate)
                    insertsys.Insert("人员基本信息", oldmodel.WorkDate == null ? null : Convert.ToDateTime(oldmodel.WorkDate).ToShortDateString(),
                        empbasic.WorkDate == null ? null : Convert.ToDateTime(empbasic.WorkDate).ToShortDateString(),
                        empbasic.ID, FangShi, "参加工作日期");
                if (oldmodel.TuiXiuDate != empbasic.TuiXiuDate)
                    insertsys.Insert("人员基本信息", oldmodel.TuiXiuDate == null ? null : Convert.ToDateTime(oldmodel.TuiXiuDate).ToShortDateString(),
                        empbasic.TuiXiuDate == null ? null : Convert.ToDateTime(empbasic.TuiXiuDate).ToShortDateString(),
                        empbasic.ID, FangShi, "退休时间");
                if (oldmodel.JuTuiXiuDate != empbasic.JuTuiXiuDate)
                    insertsys.Insert("人员基本信息", oldmodel.JuTuiXiuDate.ToString(), empbasic.JuTuiXiuDate.ToString(),
                        empbasic.ID, FangShi, "距离退休年限");
                if (oldmodel.HuoDeFangShi != empbasic.HuoDeFangShi)
                    insertsys.Insert("人员基本信息", oldmodel.HuoDeFangShi, empbasic.HuoDeFangShi,
                        empbasic.ID, FangShi, "任本区公务员身份方式");
                if (oldmodel.OrgID != empbasic.OrgID)
                    insertsys.Insert("人员基本信息", oldmodel.OrgID.ToString(), empbasic.OrgID.ToString(),
                        empbasic.ID, FangShi, "现工作单位");
                if (oldmodel.EmpStatus != empbasic.EmpStatus)
                    insertsys.Insert("人员基本信息", oldmodel.EmpStatus, empbasic.EmpStatus,
                        empbasic.ID, FangShi, "人员状态");
                if (oldmodel.StatusDate != empbasic.StatusDate)
                    insertsys.Insert("人员基本信息", oldmodel.StatusDate == null ? null : Convert.ToDateTime(oldmodel.StatusDate).ToShortDateString(),
                        empbasic.StatusDate == null ? null : Convert.ToDateTime(empbasic.StatusDate).ToShortDateString(),
                        empbasic.ID, FangShi, "身份变更日期");
            }

        }
        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            if(EditData.IBTitle == "编辑")
            {
                name.IsEnabled = true;
                cardno.IsEnabled = true;
                gender.IsEnabled = true;
                chushengriqi.IsEnabled = true;
                chushengdi.IsEnabled = true;
                jiguan.IsEnabled = true;
                native.IsEnabled = true;
                political.IsEnabled = true;
                health.IsEnabled = true;
                gongzuoriqi.IsEnabled = true;
                dengjihao.IsEnabled = true;
                rybzlx.IsEnabled = true;
                huji.IsEnabled = true;
                marriage.IsEnabled = true;
                dianhua.IsEnabled = true;
                position.IsEnabled = true;
                orgid.IsEnabled = true;
                grsf.IsEnabled = true;
                rudangshijian.IsEnabled = true;
                zhuanchang.IsEnabled = true;
                zhuanye.IsEnabled = true;
                rylb.IsEnabled = true;
                dengjishijian.IsEnabled = true;
                ygzt.IsEnabled = true;
                biangengriqi.IsEnabled = true;
                jbdw.IsEnabled = true;
                hbqgwyfs.IsEnabled = true;
                jiating.IsEnabled = true;
                EditData.IBTitle = "锁定";
            }
            else
            {
                name.IsEnabled = false;
                cardno.IsEnabled = false;
                gender.IsEnabled = false;
                chushengriqi.IsEnabled = false;
                chushengdi.IsEnabled = false;
                jiguan.IsEnabled = false;
                native.IsEnabled = false;
                political.IsEnabled = false;
                health.IsEnabled = false;
                gongzuoriqi.IsEnabled = false;
                dengjihao.IsEnabled = false;
                rybzlx.IsEnabled = false;
                huji.IsEnabled = false;
                marriage.IsEnabled = false;
                dianhua.IsEnabled = false;
                position.IsEnabled = false;
                orgid.IsEnabled = false;
                grsf.IsEnabled = false;
                rudangshijian.IsEnabled = false;
                zhuanchang.IsEnabled = false;
                zhuanye.IsEnabled = false;
                rylb.IsEnabled = false;
                dengjishijian.IsEnabled = false;
                ygzt.IsEnabled = false;
                biangengriqi.IsEnabled = false;
                jbdw.IsEnabled = false;
                hbqgwyfs.IsEnabled = false;
                jiating.IsEnabled = false;
                EditData.IBTitle = "编辑";
            }
        }

        #region 职务
        private void NewZW_Click(object sender, RoutedEventArgs e)
        {
            EmpDutyWindow RMWindow = new EmpDutyWindow(EmpID, 0, true, Convert.ToInt32(empbasic.OrgID));
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            LoadDuty();
            LoadResume();
        }
        private void EditZW_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGZW.SelectedItem as EmpbasicService.Emp_Duty;
            if(seljw != null)
            {
                EmpDutyWindow RMWindow = new EmpDutyWindow(EmpID, seljw.ID, false, Convert.ToInt32(empbasic.OrgID));
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();
            }
        }
        private void DeleteZW_Click(object sender, RoutedEventArgs e)
        {
            if(DGZW.SelectedItem == null)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要删除的信息！");
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要删除的信息！");
                return;
            }
            var seljw = this.DGZW.SelectedItem as EmpbasicService.Emp_Duty;
            if(MessageBox.Show("确定删除吗？","提示",MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                if(seljw != null)
                {
                    Client.DeteleEmp_DutyCompleted -= new EventHandler<EmpbasicService.DeteleEmp_DutyCompletedEventArgs>(Client_DeteleEmp_DutyCompleted);
                    Client.DeteleEmp_DutyCompleted += new EventHandler<EmpbasicService.DeteleEmp_DutyCompletedEventArgs>(Client_DeteleEmp_DutyCompleted);
                    Client.DeteleEmp_DutyAsync(seljw.ID);
                }
                
            }
            else SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要删除的信息！");
        }

        void Client_DeteleEmp_DutyCompleted(object sender, EmpbasicService.DeteleEmp_DutyCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");
            LoadDuty();
        }
        #endregion

        #region 简历
        private void NewJL_Click(object sender, RoutedEventArgs e)
        {
            EmpResumeWindow RMWindow1 = new EmpResumeWindow(EmpID, 0, true);
            RMWindow1.Closed += new EventHandler(RMWindow_Closed1);
            RMWindow1.Show();
        }
        void RMWindow_Closed1(object sender, EventArgs e)
        {
            LoadResume();
        }
        private void EditJL_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGJL.SelectedItem as EmpbasicService.Emp_ResumeModel;
            if (seljw != null)
            {
                EmpResumeWindow RMWindow1 = new EmpResumeWindow(EmpID, seljw.ID, false);
                RMWindow1.Closed += new EventHandler(RMWindow_Closed1);
                RMWindow1.Show();
            }
        }
        private void DeleteJL_Click(object sender, RoutedEventArgs e)
        {
            if (DGJL.SelectedItem == null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要删除的信息！");
                return;
            }
            var seljw = this.DGJL.SelectedItem as EmpbasicService.Emp_ResumeModel;
            if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                if (seljw != null)
                {
                    Client.DeteleEmp_ResumeModelCompleted -= new EventHandler<EmpbasicService.DeteleEmp_ResumeModelCompletedEventArgs>(Client_DeteleEmp_ResumeModelCompleted);
                    Client.DeteleEmp_ResumeModelCompleted += new EventHandler<EmpbasicService.DeteleEmp_ResumeModelCompletedEventArgs>(Client_DeteleEmp_ResumeModelCompleted);
                    Client.DeteleEmp_ResumeModelAsync(seljw.ID);
                }

            }
            else SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要删除的信息！");
        }

        void Client_DeteleEmp_ResumeModelCompleted(object sender, EmpbasicService.DeteleEmp_ResumeModelCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");
            LoadResume();
        }

        #endregion

        #region 学历
        private void NewXL_Click(object sender, RoutedEventArgs e)
        {
            EmpEducationWindow RMWindow2 = new EmpEducationWindow(EmpID, 0, true);
            RMWindow2.Closed += new EventHandler(RMWindow_Closed2);
            RMWindow2.Show();
        }
        void RMWindow_Closed2(object sender, EventArgs e)
        {
            LoadEducation();
        }
        private void EditXL_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGXL.SelectedItem as EmpbasicService.Emp_EducationModel;
            if (seljw != null)
            {
                EmpEducationWindow RMWindow2 = new EmpEducationWindow(EmpID, seljw.ID, false);
                RMWindow2.Closed += new EventHandler(RMWindow_Closed2);
                RMWindow2.Show();
            }
        }
        private void DeleteXL_Click(object sender, RoutedEventArgs e)
        {
            if (DGXL.SelectedItem == null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要删除的信息！");
                return;
            }
            var seljw = this.DGXL.SelectedItem as EmpbasicService.Emp_EducationModel;
            if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                if (seljw != null)
                {
                    Client.DeteleEmp_EducationModelCompleted -= new EventHandler<EmpbasicService.DeteleEmp_EducationModelCompletedEventArgs>(Client_DeteleEmp_EducationModelCompleted);
                    Client.DeteleEmp_EducationModelCompleted += new EventHandler<EmpbasicService.DeteleEmp_EducationModelCompletedEventArgs>(Client_DeteleEmp_EducationModelCompleted);
                    Client.DeteleEmp_EducationModelAsync(seljw.ID);
                }

            }
            else SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要删除的信息！");
        }

        void Client_DeteleEmp_EducationModelCompleted(object sender, EmpbasicService.DeteleEmp_EducationModelCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");
            LoadEducation();
        }

        #endregion

        #region 学位
        private void NewXW_Click(object sender, RoutedEventArgs e)
        {
            EmpDegreeWindow RMWindow2 = new EmpDegreeWindow(EmpID, 0, true);
            RMWindow2.Closed += new EventHandler(RMWindow_Closed3);
            RMWindow2.Show();
        }
        void RMWindow_Closed3(object sender, EventArgs e)
        {
            LoadDegree();
        }
        private void EditXW_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGXW.SelectedItem as EmpbasicService.Emp_DegreeModel;
            if (seljw != null)
            {
                EmpDegreeWindow RMWindow2 = new EmpDegreeWindow(EmpID, seljw.ID, false);
                RMWindow2.Closed += new EventHandler(RMWindow_Closed3);
                RMWindow2.Show();
            }
        }
        private void DeleteXW_Click(object sender, RoutedEventArgs e)
        {
            if (DGXW.SelectedItem == null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要删除的信息！");
                return;
            }
            var seljw = this.DGXW.SelectedItem as EmpbasicService.Emp_DegreeModel;
            if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                if (seljw != null)
                {
                    Client.DeteleEmp_DegreeModelCompleted -= new EventHandler<EmpbasicService.DeteleEmp_DegreeModelCompletedEventArgs>(Client_DeteleEmp_DegreeModelCompleted);
                    Client.DeteleEmp_DegreeModelCompleted += new EventHandler<EmpbasicService.DeteleEmp_DegreeModelCompletedEventArgs>(Client_DeteleEmp_DegreeModelCompleted);
                    Client.DeteleEmp_DegreeModelAsync(seljw.ID);
                }

            }
            else SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要删除的信息！");
        }

        void Client_DeteleEmp_DegreeModelCompleted(object sender, EmpbasicService.DeteleEmp_DegreeModelCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");
            LoadDegree();
        }

        #endregion

        #region 考核
        private void NewKH_Click(object sender, RoutedEventArgs e)
        {
            EmpAssessmentWindow RMWindow2 = new EmpAssessmentWindow(EmpID, 0, true);
            RMWindow2.Closed += new EventHandler(RMWindow_Closed4);
            RMWindow2.Show();
        }
        void RMWindow_Closed4(object sender, EventArgs e)
        {
            LoadAssessment();
        }
        private void EditKH_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGKH.SelectedItem as EmpbasicService.Emp_AssessmentModel;
            if (seljw != null)
            {
                EmpAssessmentWindow RMWindow2 = new EmpAssessmentWindow(EmpID, seljw.ID, false);
                RMWindow2.Closed += new EventHandler(RMWindow_Closed4);
                RMWindow2.Show();
            }
        }
        private void DeleteKH_Click(object sender, RoutedEventArgs e)
        {
            if (DGKH.SelectedItem == null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要删除的信息！");
                return;
            }
            var seljw = this.DGKH.SelectedItem as EmpbasicService.Emp_AssessmentModel;
            if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                if (seljw != null)
                {
                    Client.DeteleEmp_AssessmentModelCompleted -= new EventHandler<EmpbasicService.DeteleEmp_AssessmentModelCompletedEventArgs>(Client_DeteleEmp_AssessmentModelCompleted);
                    Client.DeteleEmp_AssessmentModelCompleted += new EventHandler<EmpbasicService.DeteleEmp_AssessmentModelCompletedEventArgs>(Client_DeteleEmp_AssessmentModelCompleted);
                    Client.DeteleEmp_AssessmentModelAsync(seljw.ID);
                }

            }
            else SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要删除的信息！");
        }

        void Client_DeteleEmp_AssessmentModelCompleted(object sender, EmpbasicService.DeteleEmp_AssessmentModelCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");
            LoadAssessment();
        }

        #endregion

        #region 奖惩
        private void NewJC_Click(object sender, RoutedEventArgs e)
        {
            EmpRewardInfoWindow RMWindow2 = new EmpRewardInfoWindow(EmpID, 0, true);
            RMWindow2.Closed += new EventHandler(RMWindow_Closed5);
            RMWindow2.Show();
        }
        void RMWindow_Closed5(object sender, EventArgs e)
        {
            LoadReward();
        }
        private void EditJC_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGJC.SelectedItem as EmpbasicService.Emp_RewardInfoModel;
            if (seljw != null)
            {
                EmpRewardInfoWindow RMWindow2 = new EmpRewardInfoWindow(EmpID, seljw.ID, false);
                RMWindow2.Closed += new EventHandler(RMWindow_Closed5);
                RMWindow2.Show();
            }
        }
        private void DeleteJC_Click(object sender, RoutedEventArgs e)
        {
            if (DGJC.SelectedItem == null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要删除的信息！");
                return;
            }
            var seljw = this.DGJC.SelectedItem as EmpbasicService.Emp_RewardInfoModel;
            if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                if (seljw != null)
                {
                    Client.DeteleEmp_RewardInfoModelCompleted += new EventHandler<EmpbasicService.DeteleEmp_RewardInfoModelCompletedEventArgs>(Client_DeteleEmp_RewardInfoModelCompleted);
                    Client.DeteleEmp_RewardInfoModelCompleted += new EventHandler<EmpbasicService.DeteleEmp_RewardInfoModelCompletedEventArgs>(Client_DeteleEmp_RewardInfoModelCompleted);
                    Client.DeteleEmp_RewardInfoModelAsync(seljw.ID);
                }

            }
            else SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要删除的信息！");
        }

        void Client_DeteleEmp_RewardInfoModelCompleted(object sender, EmpbasicService.DeteleEmp_RewardInfoModelCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");
            LoadReward();
        }
        #endregion

        #region 处分
        private void NewCF_Click(object sender, RoutedEventArgs e)
        {
            EmpPunishmentWindow RMWindow2 = new EmpPunishmentWindow(EmpID, 0, true);
            RMWindow2.Closed += new EventHandler(RMWindow_Closed6);
            RMWindow2.Show();
        }
        void RMWindow_Closed6(object sender, EventArgs e)
        {
            LoadPunish();
        }
        private void EditCF_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGCF.SelectedItem as EmpbasicService.Emp_PunishmentModel;
            if (seljw != null)
            {
                EmpPunishmentWindow RMWindow2 = new EmpPunishmentWindow(EmpID, seljw.ID, false);
                RMWindow2.Closed += new EventHandler(RMWindow_Closed6);
                RMWindow2.Show();
            }
        }
        private void DeleteCF_Click(object sender, RoutedEventArgs e)
        {
            if (DGCF.SelectedItem == null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要删除的信息！");
                return;
            }
            var seljw = this.DGCF.SelectedItem as EmpbasicService.Emp_PunishmentModel;
            if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                if (seljw != null)
                {
                    Client.DeteleEmp_PunishmentModelCompleted -= new EventHandler<EmpbasicService.DeteleEmp_PunishmentModelCompletedEventArgs>(Client_DeteleEmp_PunishmentModelCompleted);
                    Client.DeteleEmp_PunishmentModelCompleted += new EventHandler<EmpbasicService.DeteleEmp_PunishmentModelCompletedEventArgs>(Client_DeteleEmp_PunishmentModelCompleted);
                    Client.DeteleEmp_PunishmentModelAsync(seljw.ID);
                }

            }
            else SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要删除的信息！");
        }

        void Client_DeteleEmp_PunishmentModelCompleted(object sender, EmpbasicService.DeteleEmp_PunishmentModelCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");
            LoadPunish();
        }

        #endregion

        #region 家庭关系
        private void NewJT_Click(object sender, RoutedEventArgs e)
        {
            EmpFamilyRelationWindow RMWindow2 = new EmpFamilyRelationWindow(EmpID, 0, true);
            RMWindow2.Closed += new EventHandler(RMWindow_Closed7);
            RMWindow2.Show();
        }
        void RMWindow_Closed7(object sender, EventArgs e)
        {
            LoadFamily();
        }
        private void EditJT_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGJT.SelectedItem as EmpbasicService.Emp_FamilyRelationModel;
            if (seljw != null)
            {
                EmpFamilyRelationWindow RMWindow2 = new EmpFamilyRelationWindow(EmpID, seljw.ID, false);
                RMWindow2.Closed += new EventHandler(RMWindow_Closed7);
                RMWindow2.Show();
            }
        }
        private void DeleteJT_Click(object sender, RoutedEventArgs e)
        {
            if (DGJT.SelectedItem == null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要删除的信息！");
                return;
            }
            var seljw = this.DGJT.SelectedItem as EmpbasicService.Emp_FamilyRelationModel;
            if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                if (seljw != null)
                {
                    Client.DeteleEmp_FamilyRelationModelCompleted -= new EventHandler<EmpbasicService.DeteleEmp_FamilyRelationModelCompletedEventArgs>(Client_DeteleEmp_FamilyRelationModelCompleted);
                    Client.DeteleEmp_FamilyRelationModelCompleted += new EventHandler<EmpbasicService.DeteleEmp_FamilyRelationModelCompletedEventArgs>(Client_DeteleEmp_FamilyRelationModelCompleted);
                    Client.DeteleEmp_FamilyRelationModelAsync(seljw.ID);
                }

            }
            else SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要删除的信息！");
        }

        void Client_DeteleEmp_FamilyRelationModelCompleted(object sender, EmpbasicService.DeteleEmp_FamilyRelationModelCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");
            LoadFamily();
        }

        #endregion


        private Stream ExportPersonnelInfo = null;
        /// <summary>
        /// 导出人员基本信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            var downLoad = new SaveFileDialog()
            {
                //Filter = "Excel files(2007~2010) (*.xlsx)|*.xlsx|Excel files(2003) (*.xls)|*.xls",
                Filter = "Excel files(2003) (*.xls)|*.xls",

            };
            if (downLoad.ShowDialog() == true)
            {
                ExportPersonnelInfo = downLoad.OpenFile();
                List<byte> content = new List<byte>();
                string username = Session.LoginUser.LoginName;
                Client.GetPersonnelInfoExcelAsync(this.EmpID, username, content);
            

            }
        }

        void Client_GetPersonnelInfoExcelCompleted(object sender, EmpbasicService.GetPersonnelInfoExcelCompletedEventArgs e)
        {   
            List<byte> content = e.UserState as List<byte>;
            CommunitySystem.EmpbasicService.FileTransferStream fff = e.Result;
            if (fff.IsFinished == false)
            {
                content.AddRange(fff.FileContent);
                string username = Session.LoginUser.LoginName;
                Client.GetPersonnelInfoExcelAsync(this.EmpID, username, content);
            }
            else
            {
                content.AddRange(fff.FileContent);
                byte[] bytes = content.ToArray();
                ExportPersonnelInfo.Write(bytes, 0, bytes.Length);
                ExportPersonnelInfo.Close();
                ExportPersonnelInfo.Dispose();
                SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功!");
            }            
        }
    }
}
